package com.pan.crawler.auto.action;

import com.pan.download5.parse.entity.auto.impl.AutoNext;
import com.pan.crawler.auto.utils.AutoValueTools;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;

import java.util.NoSuchElementException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TurnPage {
    //是否需要翻页
    boolean isTurn;
    String xpath;
    Pattern conditionPattern = Pattern.compile("^\\s*(!=|==)\\s*(text|attr\\s*:\\s*\\w+)\\s*'([\\W\\w]*)'\\s*$");
    String clickXpath;
    //condition info
    String op;
    boolean isAttr;
    String attrName;
    String conditionValue;

    public TurnPage(String xpath, String condition, String click) {
        if (xpath.isEmpty() && condition.isEmpty()){
            isTurn = false;
            return;
        }
        this.isTurn = true;
        this.xpath = xpath;
        Matcher matcher = this.conditionPattern.matcher(condition);
        matcher.find();
        this.op = matcher.group(1);
        AutoValueTools.Value v = AutoValueTools.INSTANCE.parse(matcher.group(2));
        this.isAttr = v.isAttr();
        this.attrName = v.getAttrName();
        this.conditionValue = matcher.group(3);
        this.clickXpath = click;

    }

    public TurnPage(AutoNext autoNext){
        this(autoNext==null?null: autoNext.getXpath(),
                autoNext==null?null: autoNext.getCondition(),
                autoNext==null? null: autoNext.getClickXPath());
    }

    /**
     * 返回false就是无法再翻下一页，true就是已经翻页了
     *
     * @return
     */
    public boolean execute(EdgeDriver edgeDriver) {
        if (!isTurn){
            return false;
        }
        WebElement conditionElement;
        try {
            conditionElement = edgeDriver.findElement(By.xpath(xpath));
        } catch (NoSuchElementException e) {
            System.out.println("test 找不到节点: " + e);
            return false;
        }
        String tarValue;

        if (isAttr) {
            try {
                tarValue = conditionElement.getAttribute(attrName);
            } catch (Exception e) {
                System.out.println("test 找不到属性: " + e);
                return false;
            }
        } else {
            tarValue = conditionElement.getText();
        }
        WebElement clickButton;
        try {
            clickButton = edgeDriver.findElement(By.xpath(clickXpath.isEmpty() ? xpath : clickXpath));
        }catch (NoSuchElementException e){
            System.out.println("test 找不到下一页按钮： "+e);
            return false;
        }
        /**
         * 这是同或
         * 1 1 1
         * 1 0 0
         * 0 1 0
         * 0 0 1
         * 这种写法可能会有损扩展性，比如将来增加操作符
         */
        if (!(tarValue.equals(conditionValue) ^ op.indexOf("==")!= -1)) {
            clickButton.click();
            return true;
        } else {
            return false;
        }

    }

}
